package com.sport.setting.controller;

import com.sport.apis.annotation.SportStatus;
import com.sport.common.collect.ListUtils;
import com.sport.common.excel.ExcelExport;
import com.sport.model.application.dtos.SportMemberUpdateDto;
import com.sport.model.common.dtos.ResponseResult;
import com.sport.model.common.enums.AppHttpCodeEnum;
import com.sport.model.excel.SportGradeUnitDto;
import com.sport.model.excel.SportMemberEnrollExcelTemplate;
import com.sport.model.excel.SportMemberEnrollExport;
import com.sport.model.excel.annotation.ExcelField;
import com.sport.setting.service.SportMeetingEnrollService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/SportEnroll")
@SportStatus(sportTargetStatus = SportStatus.InnerStatus.ORGANIZE_SPORT_MEETING)
public class SportMeetingEnrollController {

    @Autowired
    SportMeetingEnrollService sportMeetingEnrollService;

    /**
     * 下载报名模板
     * @param response
     */
    @RequestMapping("/importTemplate")
    public void importEnrollTemplate(HttpServletResponse response){
        SportMemberEnrollExcelTemplate sportMemberEnrollExcelTemplate = new SportMemberEnrollExcelTemplate();
        List<SportMemberEnrollExcelTemplate> sportMemberEnrollExcelTemplates = ListUtils.newArrayList(sportMemberEnrollExcelTemplate);
        Map<String,String> titleMap=new HashMap<>();
        titleMap.put("比赛组",null);
        titleMap.put("比赛单位",null);
        String fileName="学生报名表.xlsx";
        List<String> sportItemList = (List<String>) sportMeetingEnrollService.getEventsGamesToTemplate().getData();
        try(ExcelExport ee=new ExcelExport(titleMap,SportMemberEnrollExcelTemplate.class, ExcelField.Type.IMPORT,null)){
            ee.setDataList(sportMemberEnrollExcelTemplates,sportItemList).write(response,fileName);
        }
    }


    /**
     * 导出报名数据
     */
    @RequestMapping("/exportData")
    public void exportData(HttpServletResponse response, @RequestBody SportGradeUnitDto sportGradeUnitDto){
        List<SportMemberEnrollExport> list=sportMeetingEnrollService.getSportEnrollExport(sportGradeUnitDto);
        String fileName=sportGradeUnitDto.getSportMemberGrade()+"-"+sportGradeUnitDto.getSportMemberUnit()+"报名表.xlsx";
        Map<String,String> titleMap=new HashMap<>();
        titleMap.put("比赛组",sportGradeUnitDto.getSportMemberGrade());
        titleMap.put("比赛单位",sportGradeUnitDto.getSportMemberUnit());
        try(ExcelExport ee=new ExcelExport(titleMap,SportMemberEnrollExport.class)){
            ee.setDataList(list,null).write(response,fileName);
        }
    }



    /**
     * 导出报名信息
     */
    @RequestMapping("/exportEnrollInfo")
    public ResponseResult exportEnrollInfo(@RequestBody SportGradeUnitDto sportGradeUnitDto){
        List<SportMemberEnrollExport> list=sportMeetingEnrollService.getSportEnrollExport(sportGradeUnitDto);
        if(list!=null){
            return ResponseResult.okResult(list);
        }
        return ResponseResult.errorResult(AppHttpCodeEnum.USER_DATA_NOT_EXIST);
    }


    //导入报名数据
    @RequestMapping("/importData")
    public ResponseResult importData(MultipartFile file){
        return sportMeetingEnrollService.importData(file);
    }




    /**
     * 返回单位的报名成员
     */
    @RequestMapping("/memberInUnit")
    public ResponseResult memberInUnit(@RequestParam("unit") String unit,@RequestParam("gender") int gender){
        return sportMeetingEnrollService.getMemberInUnit(unit,gender);
    }


    /**
     * 修改报名成员的报名项目
     */
    @RequestMapping("/updateEnrollItem")
    public ResponseResult updateEnrollItem(@RequestBody @Validated SportMemberUpdateDto sportMemberUpdateDto){
        return sportMeetingEnrollService.updateEnrollItem(sportMemberUpdateDto);
    }

    /**
     * 导出word
     */
    @RequestMapping("/wordExport")
    public void exportEnrollWord(HttpServletResponse response){
        String fileName="秩序册.docx";
        sportMeetingEnrollService.exportEnrollWord(response,fileName);
    }

}
